package git.bankview.wl.servlet;

import git.bankview.sl.servlet.ITransportProgressListener;
import javax.servlet.jsp.JspWriter;
import org.apache.log4j.Logger;

public class FileUploadProgressListener
	implements ITransportProgressListener
{
	private static Logger log = Logger.getLogger(FileUploadProgressListener.class);

	JspWriter m_oWriter = null;
	long m_lastOutPosition = -1;
	long m_step = -1;
	String m_funcName = null;
	
	public FileUploadProgressListener(JspWriter out, String funcName, long step)
	{
		m_oWriter = out;
		m_step = step;
		m_funcName = funcName;
	}
	
	public boolean onProgress(long position, long totalLength)
	{
		//log.debug("onProgress(" + position + ", " + totalLength + ")");
		try
		{
			if (m_step < position - m_lastOutPosition)
			{
				StringBuffer out = new StringBuffer();
				out.append("<script>").append(m_funcName).append("(")
					.append(position).append(", ").append(totalLength).append(");</script>");
				m_oWriter.println(out);
				m_oWriter.flush();
				m_lastOutPosition = position;
			}
		}
		catch(Exception e)
		{
			log.warn("output progress info error: ", e);
			return false;
		}
		
		return true;
	}
}
